/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IteratedPrisonerDilemmaGameComponent.java
 *
 * Created on 26/Dez/2011, 20:17:55
 */

package intopo.game.impl.ipd;

import java.awt.BorderLayout;

/**
 *
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 26/Dez/2011
 */
public class IteratedPrisonerDilemmaGameComponent
	extends intopo.game.gui.AbstractGameEditorComponent<IteratedPrisonerDilemmaGame, IteratedPrisonerDilemmaStrategy>
{
	/**
	 * Bit mask that indicates if there is an error in any of the game parameters.
	 */
	private int error;
	/** Creates new form BeanForm */
	public IteratedPrisonerDilemmaGameComponent ()
	{
		initComponents();
		this.add (this.infoPanel, BorderLayout.SOUTH);
		this.updateInfoPanel (true);
		error = 0;
	}
	@Override
	public String getConfigFileDescription ()
	{
		throw new UnsupportedOperationException ("Not supported yet.");
	}

	@Override
	public IteratedPrisonerDilemmaGame getGame ()
	{
		if (this.error == 0) {
			float T = (Float) this.payoff01FormattedTextField.getValue ();
			float R = (Float) this.payoff00FormattedTextField.getValue ();
			float S = (Float) this.payoff10FormattedTextField.getValue ();
			float P = (Float) this.payoff11FormattedTextField.getValue ();
			float oneMoreRoundProbability = (Float) this.oneMoreRoundProbabilityFormattedTextField.getValue ();
			int minimumNumberIterations = (Integer) this.minimumNumberIterationsFormattedTextField.getValue ();
			return new IteratedPrisonerDilemmaGame (T, R, P, S, oneMoreRoundProbability, minimumNumberIterations);
		}
		else {
			return null;
		}
	}

	@Override
	public void setGame (IteratedPrisonerDilemmaGame game)
	{
		this.payoff00FormattedTextField.setValue (new Float (game.cooperate ()));
		this.payoff01FormattedTextField.setValue (new Float (game.suckers ()));
		this.payoff10FormattedTextField.setValue (new Float (game.temptation ()));
		this.payoff11FormattedTextField.setValue (new Float (game.penalty ()));
		this.oneMoreRoundProbabilityFormattedTextField.setValue (new Float (game.getOneMoreRoundProbability ()));
		this.minimumNumberIterationsFormattedTextField.setValue (new Integer (game.getMinimumNumberOfIterations ()));
	}


	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {
      java.awt.GridBagConstraints gridBagConstraints;

      parametersPanel = new javax.swing.JPanel();
      payoffMatrixPanel = new javax.swing.JPanel();
      javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
      javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
      javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
      javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
      payoff00FormattedTextField = new javax.swing.JFormattedTextField();
      payoff10FormattedTextField = new javax.swing.JFormattedTextField();
      javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
      payoff01FormattedTextField = new javax.swing.JFormattedTextField();
      payoff11FormattedTextField = new javax.swing.JFormattedTextField();
      javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
      oneMoreRoundProbabilityFormattedTextField = new javax.swing.JFormattedTextField();
      javax.swing.JLabel jLabel7 = new javax.swing.JLabel();
      minimumNumberIterationsFormattedTextField = new javax.swing.JFormattedTextField();

      setBorder(javax.swing.BorderFactory.createTitledBorder("game parameters"));
      setLayout(new java.awt.BorderLayout());

      parametersPanel.setLayout(new java.awt.GridBagLayout());

      payoffMatrixPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("payoff matrix"));
      payoffMatrixPanel.setLayout(new java.awt.GridLayout(3, 3));
      payoffMatrixPanel.add(jLabel1);

      jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      jLabel2.setText("C");
      payoffMatrixPanel.add(jLabel2);

      jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      jLabel3.setText("D");
      payoffMatrixPanel.add(jLabel3);

      jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
      jLabel4.setText("C");
      payoffMatrixPanel.add(jLabel4);

      payoff00FormattedTextField.setColumns(5);
      payoff00FormattedTextField.setValue(new Float (3));
      payoff00FormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            payoffFormattedTextFieldPropertyChange(evt);
         }
      });
      payoffMatrixPanel.add(payoff00FormattedTextField);

      payoff10FormattedTextField.setColumns(5);
      payoff10FormattedTextField.setValue(new Float (0));
      payoff10FormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            payoffFormattedTextFieldPropertyChange(evt);
         }
      });
      payoffMatrixPanel.add(payoff10FormattedTextField);

      jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
      jLabel5.setText("D");
      payoffMatrixPanel.add(jLabel5);

      payoff01FormattedTextField.setColumns(5);
      payoff01FormattedTextField.setValue(new Float (5));
      payoffMatrixPanel.add(payoff01FormattedTextField);

      payoff11FormattedTextField.setColumns(5);
      payoff11FormattedTextField.setValue(new Float (1));
      payoffMatrixPanel.add(payoff11FormattedTextField);

      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridwidth = 2;
      parametersPanel.add(payoffMatrixPanel, gridBagConstraints);

      jLabel6.setText("one more round probability: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      parametersPanel.add(jLabel6, gridBagConstraints);

      oneMoreRoundProbabilityFormattedTextField.setColumns(5);
      oneMoreRoundProbabilityFormattedTextField.setValue(new Float (0));
      oneMoreRoundProbabilityFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            oneMoreRoundProbabilityFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      parametersPanel.add(oneMoreRoundProbabilityFormattedTextField, gridBagConstraints);

      jLabel7.setText("minimum number iterations: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      parametersPanel.add(jLabel7, gridBagConstraints);

      minimumNumberIterationsFormattedTextField.setColumns(5);
      minimumNumberIterationsFormattedTextField.setValue(new Integer (100));
      minimumNumberIterationsFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            minimumNumberIterationsFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      parametersPanel.add(minimumNumberIterationsFormattedTextField, gridBagConstraints);

      add(parametersPanel, java.awt.BorderLayout.CENTER);
   }// </editor-fold>//GEN-END:initComponents

	 private void oneMoreRoundProbabilityFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_oneMoreRoundProbabilityFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float value = (Float) this.oneMoreRoundProbabilityFormattedTextField.getValue ();
			final boolean ok = (value >= 0) && (value <= 1);
			if (ok) {
				error &= ~2;
				if (error == 0) {
					this.updateInfoPanel (true);
				}
			}
			else {
				error |= 2;
			}
			this.update (this.oneMoreRoundProbabilityFormattedTextField, ok);
		}
	 }//GEN-LAST:event_oneMoreRoundProbabilityFormattedTextFieldPropertyChange

	 private void minimumNumberIterationsFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_minimumNumberIterationsFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final int value = (Integer) this.minimumNumberIterationsFormattedTextField.getValue ();
			final boolean ok = (value >= 0);
			if (ok) {
				this.updateInfoPanel (true);
				error &= ~4;
				if (error == 0) {
					this.updateInfoPanel (true);
				}
			}
			else {
				error |= 4;
			}
			this.update (this.minimumNumberIterationsFormattedTextField, ok);
		}
	 }//GEN-LAST:event_minimumNumberIterationsFormattedTextFieldPropertyChange

	 private void payoffFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_payoffFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			float T = (Float) this.payoff01FormattedTextField.getValue ();
			float R = (Float) this.payoff00FormattedTextField.getValue ();
			float S = (Float) this.payoff10FormattedTextField.getValue ();
			float P = (Float) this.payoff11FormattedTextField.getValue ();
			final boolean ok = (T > R) && (R > P) && (P > S) && (2 * R > T + S);
			if (ok) {
				error &= ~1;
				if (error == 0) {
					this.updateInfoPanel (true);
				}
			}
			else {
				error |= 1;
			}
			this.update (this.payoff00FormattedTextField, ok);
			this.update (this.payoff01FormattedTextField, ok);
			this.update (this.payoff10FormattedTextField, ok);
			this.update (this.payoff11FormattedTextField, ok);
		}
	 }//GEN-LAST:event_payoffFormattedTextFieldPropertyChange


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JFormattedTextField minimumNumberIterationsFormattedTextField;
   private javax.swing.JFormattedTextField oneMoreRoundProbabilityFormattedTextField;
   private javax.swing.JPanel parametersPanel;
   private javax.swing.JFormattedTextField payoff00FormattedTextField;
   private javax.swing.JFormattedTextField payoff01FormattedTextField;
   private javax.swing.JFormattedTextField payoff10FormattedTextField;
   private javax.swing.JFormattedTextField payoff11FormattedTextField;
   private javax.swing.JPanel payoffMatrixPanel;
   // End of variables declaration//GEN-END:variables

}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
